/*
 * Copy right @ Ericsson Global Services Limited.
 */
package com.ericsson.mna.model.bs;
import java.sql.*;
import java.util.ArrayList;

import com.ericsson.mna.model.dao.OracleConnectionFactory;
import com.ericsson.mna.model.pojo.CreateUser;
import com.ericsson.mna.model.pojo.DBConf;
import com.ericsson.mna.model.pojo.TodoUser;

/**
 * The Class UserRegisterServices.
 */
public class UserRegisterServices {

	/** The db conf. */
	public DBConf dbConf;

/**
 * Instantiates a new user register services.
 *
 * @param DbConf the db conf
 */
public UserRegisterServices(DBConf DbConf) {
		dbConf=DbConf;
	}

/**
 * Check user.
 *
 * @param user the user
 * @param dbConf the db conf
 * @return true, if successful
 */
public static boolean checkUser(TodoUser user,DBConf dbConf) {

		boolean status=false;
		Connection con=null;
		PreparedStatement pst=null;

		//Creating connection
		OracleConnectionFactory ocf=OracleConnectionFactory.getInstance();
		con=ocf.accessDB(dbConf);

		try {
			String sql="select * from user_login_todo where user_Id=? and password=?and role=?";
			//String sql="select * from user_login_todo where user_Id=? and password=?";
			pst=con.prepareStatement(sql);
			pst.setString(1,user.getUserId());
			pst.setString(2,user.getPassword());
			pst.setInt(3,Integer.parseInt(user.getRole()));
			//pst.setBoolean(4,user.getStatus());
			ResultSet rs=pst.executeQuery();
			status=rs.next();
			System.out.println("From checkuser block .... status="+status);

		}

		catch (SQLException sqe)
		{
			System.out.println("Exception caught in BS :"+sqe);
			sqe.printStackTrace();
		}
		finally
		{
			try
			{
				con.close();
			} catch (SQLException sqe) {
				System.out.println("Exception caught in BS :"+sqe);
			}
		}
		System.out.println(status);
		return status;
	}

/**
 * Insert user.
 *
 * @param cu the cu
 * @param userId the user id
 * @return the int
 */
public  int insertUser(CreateUser cu, String userId) {
	int value=0;
	int x=enquiryIdGenerator();
	String id="EGSI"+x;
	cu.setUserId(id);


	Connection con=null;
	PreparedStatement pst=null;

	//Creating connection
	OracleConnectionFactory ocf=OracleConnectionFactory.getInstance();
	con=ocf.accessDB(dbConf);
	String sql="INSERT INTO user_details_todo VALUES (?,?,?,?,?,?,?,?,?,sysdate)";
	System.out.println(sql);
	try {
		pst=con.prepareStatement(sql);
		pst.setString(1, cu.getUserId());
		pst.setString(2, cu.getName());
		pst.setString(3, cu.getRole());
		pst.setString(4, cu.getDomain());
		pst.setString(5, cu.getSub_domain());
		pst.setString(6, cu.getPrimarySkill());
		pst.setString(7, cu.getSecondarySkill());
		pst.setString(8,"New");
		pst.setString(9,userId);
		//pst.setString(10, "1-1-15");
		value=pst.executeUpdate();
		con.commit();
	} catch (SQLException e) {

		e.printStackTrace();
	}

	 finally {
			try {
				con.close();
			} catch (SQLException sqe) {
				System.out.println("Exception caught in insertEmp :"+sqe);
			}
		}

		if(value>0)
		{
		return x;
		}
		else
		{
			return 0;
		}
}

/**
 * Enquiry id generator.
 *
 * @return the int
 */
public int enquiryIdGenerator()
{
	int id=0;
	//Creating connection
	    Connection con=null;
		OracleConnectionFactory ocf=OracleConnectionFactory.getInstance();
		con=ocf.accessDB(dbConf);
	try{
	String qry="select createUser_seq.nextval from dual";
	Statement st=con.createStatement();
	ResultSet rs= st.executeQuery(qry);
	rs.next();
	id=rs.getInt(1);
	}
	catch(SQLException sqe)
	{
		sqe.printStackTrace();

	}
	finally{
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	return id;
}

/**
 * Gets the sub domain name.
 *
 * @param domainName the domain name
 * @return the sub domain name
 */
public ResultSet getSubDomainName(String domainName)
{
	Connection con=null;
	ResultSet rs = null;
	PreparedStatement pst=null;

	//Creating connection
	OracleConnectionFactory ocf=OracleConnectionFactory.getInstance();
	con=ocf.accessDB(dbConf);
	String sql="SELECT SUB_DOMAIN FROM DOMAIN_TODO WHERE DOMAIN_NAME=?";
	System.out.println(sql);
	try {
		pst=con.prepareStatement(sql);
		pst.setString(1, domainName);
		rs=pst.executeQuery();

	} catch (SQLException e) {

		e.printStackTrace();
	}
		return rs;
}
 public  int getRole(String usrId)
 {
	 Connection con=null;
		ResultSet rs = null;
		PreparedStatement pst=null;
		int role=0;
		//Creating connection
		OracleConnectionFactory ocf=OracleConnectionFactory.getInstance();
		con=ocf.accessDB(dbConf);
		String sql="SELECT role from user_details_todo where user_id=?";
		/*System.out.println(sql);*/
		try {
			pst=con.prepareStatement(sql);
			pst.setString(1, usrId);
			rs=pst.executeQuery();
			rs.next();
			role=rs.getInt(1);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	 return role;
 }

}
